package io;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * <p>This class needs the name of the input file and generates a ArrayList of Strings
 * out of the lines.<br />
 * Several Methods for accessing the lines of the input file and getting information about those
 * are offered.</p>
 * <p><br />
 * <strong>Change Log</strong><br />
 * <u>version 0.1:</u>
 * <ul><li>Methods: getLine, getLineCount, getLineLength, toString</li></ul>
 * </p>
 * 
 * @author Torbjoern Klatt
 * @date 2009-10-06
 * @version 0.1
 */
public class FileInput {
	private File f;
	private Scanner in;
	private ArrayList<String> arr = new ArrayList<String>();

	/**
	 * Creates a FileInput object of the given file
	 * @param fileName
	 */
	public FileInput(String fileName) {
		try {
			f = new File(fileName);
			in = new Scanner(f);
		
			while(in.hasNext()) {
				arr.add(in.nextLine());
			}
		}
		catch(FileNotFoundException fnfe) {
			System.out.println("File " + fileName + " not found.");
		}
	}
	
	/**
	 * Returns this particular line of the input file
	 * @param line
	 * @return a string containing the requested line
	 * @throws IndexOutOfBoundsException
	 */
	public String getLine(int line) throws IndexOutOfBoundsException {
		if((line >= 0) && (line < arr.size())) return arr.get(line);
		else throw new IndexOutOfBoundsException();
	}
	
	/**
	 * Returns the total amount of lines in this input file
	 * @return the total amount of lines of the input file
	 */
	public int getLineCount() {
		return arr.size();
	}

	/**
	 * Returns the length of this particular line of the input file
	 * @param line
	 * @return the length of that particular line
	 * @throws IndexOutOfBoundsException
	 */
	public int getLineLength(int line) throws IndexOutOfBoundsException {
		if((line >= 0) && (line < arr.size())) return arr.get(line).length();
		else throw new IndexOutOfBoundsException();
	}
	
	/**
	 * Returns a String equal to the input file
	 * @return a String equal to the input file
	 */
	@Override
	public String toString() {
		StringBuffer sb = new StringBuffer();
		for(String s : arr) {
			sb.append(s);
			sb.append("\n");
		}
		return sb.toString();
	}
}
